﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Business;
using MM.Entities;

namespace MM
{
    public partial class frmHienthiPhieuDH : Form
    {
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";
        List<CHI_TIET_PDH> lstCTPDH = new List<CHI_TIET_PDH>();

        public frmHienthiPhieuDH(string sConn, string POThamChieuSAP)
        {
            InitializeComponent();
            _sConn = sConn;

            _matau = frmDangNhap.objTau._tenTau2KiTu;


            if (POThamChieuSAP == "")
            {
                HienthiPHIEU_DAT_HANG_MAX(_matau);
                POThamChieuSAP = this.txtSoPhieuPO.Text;
            }
            else
                HienthiPHIEU_DAT_HANG(POThamChieuSAP);
            HienthiCHI_TIET_PDH(POThamChieuSAP);
            HienThiCHI_TIET_GD_ND(POThamChieuSAP);
        }

        public frmHienthiPhieuDH(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;

            _matau = frmDangNhap.objTau._tenTau2KiTu;

            string POThamChieuSAP = "";

            if (POThamChieuSAP == "")
            {
                HienthiPHIEU_DAT_HANG_MAX(_matau);
                POThamChieuSAP = this.txtSoPhieuPO.Text;
            }
            else
                HienthiPHIEU_DAT_HANG(POThamChieuSAP);
            HienthiCHI_TIET_PDH(POThamChieuSAP);
            HienThiCHI_TIET_GD_ND(POThamChieuSAP);
        }

        private void HienthiPHIEU_DAT_HANG_MAX(string matau)
        {
            clsPHIEU_DAT_HANG pdhCtrl = new clsPHIEU_DAT_HANG(_sConn);
            List<PHIEU_DAT_HANG> lst = new List<PHIEU_DAT_HANG>();
            lst = pdhCtrl.usp_SelectPHIEU_DAT_HANG_MAX(matau);
            if (lst.Count > 0)
            {
                this.txtSoPhieuPO.Text = lst[0].POThamChieuSAP;
                this.txtPlant.Text = lst[0].PlanReferenceSAP;
                this.txtNguoiThucHien.Text = lst[0].NguoiNhap;
                this.txtNgayThucHien.Text = lst[0].NgayNhap.Day.ToString() + "/" + lst[0].NgayNhap.Month.ToString() + "/" + lst[0].NgayNhap.Year.ToString();
                this.lblLoaiPO.Text = lst[0].LoaiPO=="NB"?"ĐẶT MỚI":"LỆNH CHUYỂN KHO";
            }
        }

        private void HienthiPHIEU_DAT_HANG(string POThamChieuSAP)
        {
            clsPHIEU_DAT_HANG pdhCtrl = new clsPHIEU_DAT_HANG(_sConn);
            List<PHIEU_DAT_HANG> lst = new List<PHIEU_DAT_HANG>();
            lst = pdhCtrl.usp_SelectPHIEU_DAT_HANG(POThamChieuSAP);
            if (lst.Count > 0)
            {
                this.txtSoPhieuPO.Text = lst[0].POThamChieuSAP;
                this.txtPlant.Text = lst[0].PlanReferenceSAP;
                this.txtNguoiThucHien.Text = lst[0].NguoiNhap;
                this.txtNgayThucHien.Text = lst[0].NgayNhap.Day.ToString() + "/" + lst[0].NgayNhap.Month.ToString() + "/" + lst[0].NgayNhap.Year.ToString();
                this.lblLoaiPO.Text = lst[0].LoaiPO == "NB" ? "ĐẶT MỚI" : "LỆNH CHUYỂN KHO";
            }
            else
            {
                this.txtSoPhieuPO.Text = "";
                this.txtPlant.Text = "";
                this.txtNguoiThucHien.Text = "";
                this.txtNgayThucHien.Text = "";
                this.lblLoaiPO.Text = "";
                MessageBox.Show("KHÔNG TỒN TẠI SỐ PHIẾU PO NÀY:" + POThamChieuSAP);
            }
        }
        private void HienthiCHI_TIET_PDH(string POThamChieuSAP)
        {
            clsCHI_TIET_PDH ctCtrl = new clsCHI_TIET_PDH(_sConn);
           // List<CHI_TIET_PDH> lst = new List<CHI_TIET_PDH>();
            lstCTPDH = ctCtrl.usp_SelectCHI_TIET_PDHByPOThamChieuSAP(POThamChieuSAP);
            this.gridControl1.DataSource = lstCTPDH;
            this.lblSoLuong.Text = "Tổng số dòng: "+lstCTPDH.Count.ToString();
        }
        private void HienThiCHI_TIET_GD_ND(string POThamChieuSAP)
        {
            try
            {
                if (POThamChieuSAP != "")
                {
                    clsCHI_TIET_ND_GT gtCtrl = new clsCHI_TIET_ND_GT(_sConn);
                    List<CHI_TIET_ND_GT> lstGT = new List<CHI_TIET_ND_GT>();
                    lstGT = gtCtrl.usp_SelectCHI_TIET_ND_GT_byMaGiaoDich(POThamChieuSAP);

                    this.gridControl2.DataSource = lstGT;
                }
                else
                    this.gridControl2.DataSource = null;
               
            }
            catch { }
        }

        private void txtSoPhieuPO_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string POThamChieuSAP = this.txtSoPhieuPO.Text.Trim();
                HienthiPHIEU_DAT_HANG(POThamChieuSAP);
                HienthiCHI_TIET_PDH(POThamChieuSAP);
                HienThiCHI_TIET_GD_ND(POThamChieuSAP);
            }
        }

        private void lnkSoPO_Click(object sender, EventArgs e)
        {
            frmTimPhieuDH f = new frmTimPhieuDH(_sConn);
            f.chonPDH += new frmTimPhieuDH.ChonPhieuDatHang(GetPhieuDatHang);
            f.Show(this);
            
        }

        private void GetPhieuDatHang(object sender, ChonPhieuDHEventArgs e)
        {
            HienthiPHIEU_DAT_HANG(e.POThamChieuSAP);
            HienthiCHI_TIET_PDH(e.POThamChieuSAP);
            HienThiCHI_TIET_GD_ND(e.POThamChieuSAP);
        }

        private void frmHienthiPhieuDH_Paint(object sender, PaintEventArgs e)
        {
            //Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void frmHienthiPhieuDH_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void cmdIn_Click(object sender, EventArgs e)
        {
            try
            {
                List<CHI_TIET_PDH> lst = new List<CHI_TIET_PDH>(lstCTPDH);

                lst.RemoveAll(delegate(CHI_TIET_PDH x) { return x.TinhTrang == 1; });

                if (lst.Count > 0)
                { 

                   

                    clsTHAM_SO tsCtrl = new clsTHAM_SO(_sConn);
                    THAM_SO objTS = new THAM_SO();

                    objTS = tsCtrl.SelectTHAM_SO("vThuocTinh_PDH");
                    if (objTS != null & objTS.GiaTri != "")
                    {
                        string[] arrThamSo = new string[20];
                        clsTHUOC_TINH ttCtrl = new clsTHUOC_TINH(_sConn);
                        List<THUOC_TINH> lstTT = new List<THUOC_TINH>();
                        arrThamSo = objTS.GiaTri.Split(',');

                        for (int i = 0; i < lst.Count; i++)
                        {
                            lstTT.Clear();
                            lstTT = ttCtrl.SelectTHUOC_TINHByID(lst[i].MaThietBi);
                           
                            foreach (THUOC_TINH t in lstTT)
                            {
                                if (Array.IndexOf(arrThamSo, t._maLoaiThuocTinh) >= 0)
                                {
                                    if (t._giaTriTT != "" && t._giaTriTT != null)
                                    {
                                        lst[i].TenCacThuocTinh += t._tenLoaiTT + ":" + t._giaTriTT + ";";
                                    }
                                }
                            }
                        }
                    }

                    DonDatHang rpt = new DonDatHang();
                    rpt.SetDataSource(lstCTPDH);
                    rpt.SetParameterValue("prSoPO", lstCTPDH[0].POThamChieuSAP);
                    rpt.SetParameterValue("prMaDV", frmDangNhap.objTau._tenTau);
                    rpt.SetParameterValue("prHoTen", frmDangNhap.objTaiKhoan.hoTen +" - " + frmDangNhap.objTaiKhoan.soCMND);
                   


                    frmInBaoCao f = new frmInBaoCao(rpt, false);
                    f.ShowDialog();
                }
            }
            catch { }
        }

        
    }
}